<template>
    <a-spin :spinning="confirmLoading">
      <j-form-container :disabled="formDisabled">
      <div slot="detail">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
      <!--  <a-form-model-item label="版本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lockVersion">
          <a-input-number v-model="model.lockVersion" placeholder="请输入版本" style="width: 100%" />
        </a-form-model-item>
        <a-form-model-item label="删除标记" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="delFlag">
          <j-dict-select-tag type="list" v-model="model.delFlag"  dictCode="mk_whether" placeholder="请选择删除标记" />
        </a-form-model-item>-->
        <a-form-model-item label="上级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parentId">
          <j-tree-select
            ref="treeSelect"
            placeholder="请选择上级"
            v-model="model['parentId']"
            dict="mk_shop_channel,name,id"
            pidField="parent_id"
            pidValue="0"
            hasChildField="has_child"
            :condition="condition"
            >
          </j-tree-select>
        </a-form-model-item>
        <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
          <a-input v-model="model.name" placeholder="请输入名称" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="图标" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="icon">
          <j-image-upload isMultiple biz-path="laile2/system" :number="1" v-model="model.icon" ></j-image-upload>
        </a-form-model-item>
        <a-form-model-item label="首页显示" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isIndex">
          <j-dict-select-tag type="list" v-model="model.isIndex"  dictCode="mk_whether" placeholder="请选择首页显示" />
        </a-form-model-item>

      </a-form-model>
      </div>
    </j-form-container>
    </a-spin>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import store from '@/store'

  export default {
    name: "ShopChannelModal",
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
           lockVersion: [
              { required: true, message: '请输入版本!'},
           ],
           delFlag: [
              { required: true, message: '请输入删除标记!'},
           ],
           name: [
              { required: true, message: '请输入名称!'},
           ],
           icon: [
              { required: true, message: '请输入图标!'},
           ],
           isIndex: [
              { required: true, message: '请输入首页显示!'},
           ],
        },
        url: {
          add: "/shop/shopChannel/add",
          edit: "/shop/shopChannel/edit",
        },
        expandedRowKeys:[],
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      },
      condition() {
        if (store.getters.userInfoSite) {
          return `{"site_id": "${store.getters.userInfoSite.id}"}`
        } else { return '{}' }
      }
    },
    created () {
       //备份model原始值
       this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add (record) {
        console.log(record)
        this.model = Object.assign({}, record);
      },
      edit (record) {
        this.model = Object.assign({}, record);
      },
      submitForm () {
        const that = this;
        // 触发表单验证
       this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            if(this.model.id && this.model.id === this.model.parentId){
              that.$message.warning("父级节点不能选择自己");
              that.confirmLoading = false;
              return;
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                this.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
             return false
          }
        })
      },
      // submitSuccess(formData,flag){
      //   if(!formData.id){
      //     let treeData = this.$refs.treeSelect.getCurrTreeData()
      //     this.expandedRowKeys=[]
      //     this.getExpandKeysByPid(formData[this.pidField],treeData,treeData)
      //     this.$emit('ok',formData,this.expandedRowKeys.reverse());
      //   }else{
      //     this.$emit('ok',formData,flag);
      //   }
      // },
      // getExpandKeysByPid(pid,arr,all){
      //   if(pid && arr && arr.length>0){
      //     for(let i=0;i<arr.length;i++){
      //       if(arr[i].key==pid){
      //         this.expandedRowKeys.push(arr[i].key)
      //         this.getExpandKeysByPid(arr[i]['parentId'],all,all)
      //       }else{
      //         this.getExpandKeysByPid(pid,arr[i].children,all)
      //       }
      //     }
      //   }
      // }


    }
  }
</script>